package com.example.teacher.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.teacher.R

/**
 *@Created by 覃德玮 on 2021/9/10 09:31
 *@Email: chadewik@foxmail.com
 *@Description:
 */
class DrawerAdapter : BaseAdapter() {
    private val item = arrayOf("首页", "课程", "历史考勤")
    private val itemImage =
        arrayOf(R.drawable.ic_home, R.drawable.ic_course, R.drawable.ic_attendance)
    var selestedItemPos = -1

    override fun getCount(): Int {
        return item.size
    }

    override fun getItem(position: Int): Any {
        return item[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var viewHolder: DrawerViewHodler = DrawerViewHodler()
        var view = LayoutInflater.from(parent?.context).inflate(R.layout.item, parent, false)
        selestedItemPos = -1

        viewHolder.tv = view?.findViewById(R.id.title)
        viewHolder.icon = view?.findViewById(R.id.imagetitle)
        viewHolder.tv?.setText(item[position]);
        viewHolder.icon?.setImageResource(itemImage[position])
        view?.setTag(viewHolder)

//        viewHolder = view.tag as DrawerViewHodler
        viewHolder.tv?.setText(item[position]);
        viewHolder.icon?.setImageResource(itemImage[position])

        //选中设置字体为高亮显示
        if (selestedItemPos == position) {
            viewHolder.tv?.setTextColor(
                ContextCompat.getColor(
                    parent?.context!!, R.color.purple_200
                )
            );
        } else {
            viewHolder.tv?.setTextColor(ContextCompat.getColor(parent?.context!!, R.color.black));

        }
        return view!!
    }

    fun setSelectedItem(i: Int) {
        this.selestedItemPos = i
    }

    class DrawerViewHodler {
        var tv: TextView? = null
        var icon: ImageView? = null
    }
}